package com.yanqu.road.server.cmd.grandchild.born;

import com.yanqu.road.entity.grandchild.GrandChildData;
import com.yanqu.road.pb.grandchild.GrandChildProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.grandson.GrandChildModule;
import com.yanqu.road.server.manger.config.grandchild.GrandChildConfigMgr;
import com.yanqu.road.server.pb.GrandChildPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.List;
import java.util.Set;

@Cmd(code = GameProtocol.S_CONFIRM_GRANDSON_DATA,desc = "确认保存孙子数据")
public class SaveGrandChildCmd extends AbstractCommand {
    private String id;
    private String nickName;
    private List<Integer> talentList;
    private GrandChildData oldClone;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        GrandChildProto.ConfirmGrandChildReqMsg req = GrandChildProto.ConfirmGrandChildReqMsg.parseFrom(packet.getBytes());
        id = req.getId();
        nickName = req.getNickName();
        talentList = req.getTalentIdList();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        GrandChildData grandChild = player.getModule(GrandChildModule.class).getGrandChild(id);
        oldClone = grandChild.clone();
        ret = player.getModule(GrandChildModule.class).saveGrandChild(id, nickName, talentList);
    }

    @Override
    public void sendData(GamePlayer player) {
        GrandChildProto.ConfirmGrandChildRespMsg.Builder resp = GrandChildProto.ConfirmGrandChildRespMsg.newBuilder();
        resp.setRet(ret);
        if (ret == 0) {
            GrandChildData grandChild = player.getModule(GrandChildModule.class).getGrandChild(id);
            GrandChildProto.GrandChildMsg.Builder builder = GrandChildPb.parseGrandChildMsg(grandChild);
            resp.setGrandChild(builder);
            oldClone.setTalentList(talentList);
            Set<Integer> activeList = GrandChildConfigMgr.previewActiveTalentList(oldClone,talentList);
            resp.addAllActiveTalent(activeList);
        }
        player.sendPacket(ClientProtocol.U_CONFIRM_GRANDSON_DATA, resp);
    }
}
